package com.ryder.petmatediarybackend.module.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.shop.dto.CreateShopDTO;
import com.ryder.petmatediarybackend.module.shop.dto.UpdateShopDTO;
import com.ryder.petmatediarybackend.module.shop.service.ShopInfoService;
import com.ryder.petmatediarybackend.module.shop.vo.ShopInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：店铺信息管理 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-31
 */
@Slf4j
@Tag(name = "店铺信息管理", description = "店铺信息相关接口（需要登录 + 店铺经营者角色）")
@RestController
@RequestMapping("/shop")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class ShopInfoController {

    private final ShopInfoService shopInfoService;

    @Operation(summary = "创建店铺", description = "店铺经营者创建店铺，需要 shop:info:create 权限")
    @SaCheckPermission("shop:info:create")
    @PostMapping("/create")
    public Result<ShopInfoVO> createShop(@Valid @RequestBody CreateShopDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[店铺管理] 用户ID={} 执行创建店铺, 方法=createShop, 入参={}", userId, dto);
        ShopInfoVO vo = shopInfoService.createShop(userId, dto);
        log.info("[店铺管理] 用户ID={} 创建店铺成功, 店铺ID={}, 店铺名称={}", userId, vo.getId(), vo.getShopName());
        return Result.ok(vo);
    }

    @Operation(summary = "获取我的店铺信息", description = "获取当前用户的店铺信息，需要 shop:info:view 权限")
    @SaCheckPermission("shop:info:view")
    @GetMapping("/my-shop")
    public Result<ShopInfoVO> getMyShop() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[店铺管理] 用户ID={} 执行获取我的店铺信息, 方法=getMyShop", userId);
        ShopInfoVO vo = shopInfoService.getMyShop(userId);
        return Result.ok(vo);
    }

    @Operation(summary = "检查是否有店铺", description = "检查当前用户是否已创建店铺")
    @GetMapping("/has-shop")
    public Result<Boolean> hasShop() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[店铺管理] 用户ID={} 执行检查是否有店铺, 方法=hasShop", userId);
        boolean hasShop = shopInfoService.hasShop(userId);
        return Result.ok(hasShop);
    }

    @Operation(summary = "更新店铺信息", description = "更新店铺基本信息，需要 shop:info:update 权限")
    @SaCheckPermission("shop:info:update")
    @PutMapping("/update")
    public Result<ShopInfoVO> updateShop(@Valid @RequestBody UpdateShopDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[店铺管理] 用户ID={} 执行更新店铺信息, 方法=updateShop, 入参={}", userId, dto);
        ShopInfoVO vo = shopInfoService.updateShop(userId, dto);
        log.info("[店铺管理] 用户ID={} 更新店铺信息成功, 店铺ID={}", userId, vo.getId());
        return Result.ok(vo);
    }

    @Operation(summary = "删除店铺", description = "删除店铺（逻辑删除），需要 shop:info:delete 权限")
    @SaCheckPermission("shop:info:delete")
    @DeleteMapping("/delete")
    public Result<Void> deleteShop() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[店铺管理] 用户ID={} 执行删除店铺, 方法=deleteShop", userId);
        shopInfoService.deleteShop(userId);
        log.info("[店铺管理] 用户ID={} 删除店铺成功", userId);
        return Result.ok();
    }

    @Operation(summary = "获取附近店铺列表", description = "根据经纬度获取附近的店铺列表（公开接口）")
    @GetMapping("/nearby")
    public Result<List<ShopInfoVO>> getNearbyShops(
            @RequestParam Double longitude,
            @RequestParam Double latitude,
            @RequestParam(defaultValue = "5.0") Double radius) {
        log.info("[店铺管理] 获取附近店铺列表, 经度={}, 纬度={}, 半径={}km", longitude, latitude, radius);
        List<ShopInfoVO> shops = shopInfoService.getNearbyShops(longitude, latitude, radius);
        log.info("[店铺管理] 获取附近店铺列表成功, 数量={}", shops.size());
        return Result.ok(shops);
    }

    @Operation(summary = "获取所有营业中的店铺列表", description = "获取所有审核通过且营业中的店铺（公开接口）")
    @GetMapping("/list")
    public Result<List<ShopInfoVO>> getAllShops() {
        log.info("[店铺管理] 获取所有营业中店铺列表");
        List<ShopInfoVO> shops = shopInfoService.getAllActiveShops();
        log.info("[店铺管理] 获取店铺列表成功, 数量={}", shops.size());
        return Result.ok(shops);
    }

    @Operation(summary = "获取店铺详情", description = "根据店铺ID获取店铺详情（公开接口）")
    @GetMapping("/detail/{shopId}")
    public Result<ShopInfoVO> getShopDetail(@PathVariable Long shopId) {
        log.info("[店铺管理] 获取店铺详情, shopId={}", shopId);
        ShopInfoVO vo = shopInfoService.getShopDetail(shopId);
        return Result.ok(vo);
    }
}

